package com.bwf.local;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/23 9:28
 * @Copyright 博为峰
 */
public class ThreadLocalDemo {

    public static int a = 0;
    public static ThreadLocal<Integer> local = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
//                    System.out.println(Thread.currentThread().getName() + ",i=" + i);
//                    System.out.println(Thread.currentThread().getName() + ",a=" + a++);
                    local.set(local.get() + 1);
                }
                System.out.println(Thread.currentThread().getName() + "," + local.get());
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
//                    System.out.println(Thread.currentThread().getName() + ",i=" + i);
//                    System.out.println(Thread.currentThread().getName() + ",a=" + a++);
                    local.set(local.get() + 1);
                }
                System.out.println(Thread.currentThread().getName() + "," + local.get());
            }
        }).start();
    }
}
